import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { TaskType, TaskStatus } from '../../common/enums';
import { User } from '../../users/entities/user.entity';
import { TaskTemplate } from './task-template.entity';
import { TaskCategory } from './task-category.entity';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

@Entity('tasks')
export class Task {
  @ApiProperty({
    description: 'Unique identifier for the task',
    format: 'uuid',
    example: '123e4567-e89b-12d3-a456-426614174000',
  })
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ApiProperty({
    description: 'Task title',
    maxLength: 128,
    example: 'Create monthly report',
  })
  @Column({ length: 128 })
  title: string;

  @ApiPropertyOptional({
    description: 'Task description',
    example: 'Create a detailed monthly report for the marketing department',
  })
  @Column({ type: 'text', nullable: true })
  description: string;

  @ApiProperty({
    description: 'Task type (single or global)',
    enum: TaskType,
    default: TaskType.SINGLE,
    example: TaskType.SINGLE,
  })
  @Column({
    type: 'enum',
    enum: TaskType,
    default: TaskType.SINGLE,
  })
  type: TaskType;

  @ApiProperty({
    description: 'Task category ID',
    format: 'uuid',
    example: '123e4567-e89b-12d3-a456-426614174000',
  })
  @Column({ name: 'task_category_id' })
  taskCategoryId: string;

  @ApiPropertyOptional({
    description: 'Task category',
    type: () => TaskCategory,
  })
  @ManyToOne(() => TaskCategory, (category) => category.tasks, {
    createForeignKeyConstraints: false,
  })
  @JoinColumn({ name: 'task_category_id' })
  taskCategory: TaskCategory;

  @ApiPropertyOptional({
    description: 'Parent task ID if this is a subtask',
    format: 'uuid',
    example: '123e4567-e89b-12d3-a456-426614174000',
  })
  @Column({ name: 'parent_task_id', nullable: true })
  parentTaskId: string;

  @ApiPropertyOptional({
    description: 'Parent task',
    type: () => Task,
  })
  @ManyToOne(() => Task, { nullable: true, createForeignKeyConstraints: false })
  @JoinColumn({ name: 'parent_task_id' })
  parentTask: Task;

  @ApiPropertyOptional({
    description: 'User ID to whom the task is assigned',
    format: 'uuid',
    example: '123e4567-e89b-12d3-a456-426614174000',
  })
  @Column({ name: 'assigned_user_id', nullable: true })
  assignedUserId: string;

  @ApiPropertyOptional({
    description: 'Assigned user',
    type: () => User,
  })
  @ManyToOne(() => User, { createForeignKeyConstraints: false })
  @JoinColumn({ name: 'assigned_user_id' })
  assignedUser: User;

  @ApiPropertyOptional({
    description: 'Task start date and time',
    format: 'date-time',
    example: '2025-01-01T09:00:00Z',
  })
  @Column({ name: 'start_time', nullable: true })
  startTime: Date;

  @ApiProperty({
    description: 'Task status',
    enum: TaskStatus,
    default: TaskStatus.NOT_START,
    example: TaskStatus.NOT_START,
  })
  @Column({
    type: 'enum',
    enum: TaskStatus,
    default: TaskStatus.NOT_START,
  })
  status: TaskStatus;

  @ApiPropertyOptional({
    description: 'Task due date and time',
    format: 'date-time',
    example: '2025-12-31T23:59:59Z',
  })
  @Column({ name: 'due_time', nullable: true })
  dueTime: Date;

  @ApiPropertyOptional({
    description: 'Date and time when the task was completed',
    format: 'date-time',
    example: '2025-12-31T23:59:59Z',
  })
  @Column({ name: 'completed_at', nullable: true })
  completedAt: Date;

  @ApiPropertyOptional({
    description: 'Cron expression for recurring tasks',
    example: '0 9 * * 1-5',
  })
  @Column({ name: 'cron_expression', nullable: true, length: 32 })
  cronExpression: string;

  @ApiProperty({
    description: 'Date and time when the task was created',
    format: 'date-time',
    example: '2023-01-01T00:00:00Z',
  })
  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @ApiProperty({
    description: 'Date and time when the task was last updated',
    format: 'date-time',
    example: '2023-01-02T00:00:00Z',
  })
  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;

  @ApiProperty({
    description: 'Whether the task is deleted',
    default: false,
    example: false,
  })
  @Column({ name: 'is_deleted', default: false })
  isDeleted: boolean;

  @ApiPropertyOptional({
    description: 'Task template ID if this task is created from a template',
    format: 'uuid',
    example: '123e4567-e89b-12d3-a456-426614174000',
  })
  @Column({ name: 'task_template_id', nullable: true })
  taskTemplateId: string;

  @ApiPropertyOptional({
    description: 'Task template',
    type: () => TaskTemplate,
  })
  @ManyToOne(() => TaskTemplate, (template) => template.tasks, {
    createForeignKeyConstraints: false,
  })
  @JoinColumn({ name: 'task_template_id' })
  taskTemplate: TaskTemplate;
}
